﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System;
using System.Configuration;
using System.Net;
using System.Web;
using DotNetOpenAuth.ApplicationBlock;
using DotNetOpenAuth.ApplicationBlock.Facebook;
using DotNetOpenAuth.OAuth2;
using PaymentBLL;

public partial class pages_Facebook : System.Web.UI.Page
{
    private static readonly FacebookClient client = new FacebookClient
    {
        ClientIdentifier = ConfigurationManager.AppSettings["facebookAppID"],
        ClientCredentialApplicator = ClientCredentialApplicator.PostParameter(ConfigurationManager.AppSettings["facebookAppSecret"]),
    };

    protected void Page_Load(object sender, EventArgs e)
    {
        IAuthorizationState authorization = client.ProcessUserAuthorization();
        if (authorization == null)
        {
            // Kick off authorization request
            client.RequestUserAuthorization();
        }
        else
        {
            var request = WebRequest.Create("https://graph.facebook.com/me?access_token=" + Uri.EscapeDataString(authorization.AccessToken));
            using (var response = request.GetResponse())
            {
                using (var responseStream = response.GetResponseStream())
                {
                    var graph = FacebookGraph.Deserialize(responseStream);
                    var ctr=new CtrAccount();
                    var info = ctr.SelectAccountInfoByName(graph.Id.ToString());
                    if(info==null)
                    {
                        int? id = 0;
                        string fullName =
                            graph.Name.Trim().Equals(string.Empty)
                                ? (graph.FirstName + graph.LastName)
                                : graph.Name;
                        if(ctr.InsertAccount(graph.Id.ToString(), graph.Name, 2,fullName
                                              , "", 1, ref id)<=0)
                            return;
                        if(id>0)
                        {
                            AccountSession.Login(id.Value, graph.Id.ToString(),fullName,2);
                        }
                        else
                        {
                            AccountSession.Logout();
                        }
                    }
                    else
                    {
                        if (info.Status > 0)
                        {
                            AccountSession.Login(info.Id, info.Name, info.FullName, info.TypeId);
                        }
                        else
                        {
                            AccountSession.Logout();
                        }
                    }
                    this.nameLabel.Text = HttpUtility.HtmlEncode(graph.Name);
                }
            }
        }
    }
}